﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class LogController : Base
{
    private readonly LogService _logService;
    private readonly LogLoginService _logLoginService;
    private readonly ApplicationContext _applicationContext;
    public LogController(ApplicationContext applicationContext, LogService logService, LogLoginService logLoginService)
    {
        _applicationContext = applicationContext;
        _logService = logService;
        _logLoginService = logLoginService;
    }

    #region 列表操作日志
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Log_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery] LogSqlQuery query)
    {
        int userId = _applicationContext.ILoginUserInfoMethods.LoginUserId();
        int groupId = _applicationContext.ILoginUserInfoMethods.LoginGroupId();
        query.Uid = groupId != 1 ? userId : 0;//超级管理员显示全部数据

        if (query.ExportExcel)
        {
            PagedInfo<ViewLogSql> list = await _logService.GetSqlListAsync(query);
            string sFileName = ExcelHelperAction.ExportExcel(list.Result, "数据库操作日志");
            return Success(new { FilePath = "/export/" + sFileName, fileName = sFileName });
        }
        else
        {
            PagedInfo<ViewLogSql> list = await _logService.GetSqlListAsync(query);
            return Success(new { data = list.Result, count = list.TotalNum });
        }
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Log_Index)]
    public async Task<IActionResult> IndexLoginAsync([FromQuery] LogLoginQuery query)
    {
        int userId = _applicationContext.ILoginUserInfoMethods.LoginUserId();
        int groupId = _applicationContext.ILoginUserInfoMethods.LoginGroupId();
        query.Uid = groupId != 1 ? userId : 0;//超级管理员显示全部数据
        if (query.ExportExcel)
        {
            PagedInfo<ViewLogLogin> list = await _logLoginService.GetSqlListAsync(query);
            string sFileName = ExcelHelperAction.ExportExcel(list.Result, "用户登录日志");
            return Success(new { FilePath = "/export/" + sFileName, fileName = sFileName });
        }
        else
        {
            PagedInfo<ViewLogLogin> list = await _logLoginService.GetSqlListAsync(query);
            return Success(new { data = list.Result, count = list.TotalNum });
        }

    }
    #endregion


    #region 清除日志

    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.Log_Clear)]
    [Log(Name = "清除日志")]
    public IActionResult Clear()
    {
        if (_logService.Clear())
            return Success("清除数据库操作日志成功！");
        else
            return Fail("清除数据库操作日志失败");
    }

    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.Log_Clear)]
    [Log(Name = "清除登录日志")]
    public IActionResult ClearLogin()
    {
        if (_logLoginService.Clear())
            return Success("清除登录日志成功！");
        else
            return Fail("清除登录日志失败");
    }
    #endregion
}
